package com.gaogzhen.appScenarios.transfer;

import lombok.extern.slf4j.Slf4j;
import java.util.Random;

/**
 * @author Administrator
 * @version 1.0
 * @description 测试
 * @date 2022-10-15 11:25
 */
@Slf4j(topic = "c.TestTransfer")
public class TestTransfer {
    public static void main(String[] args) throws InterruptedException {
        Account a = new Account(1000);
        Account b = new Account(1000);

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                a.transfer(b, randomAmount());
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                b.transfer(a, randomAmount());
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();

        // 转账200次后的总金额
        log.debug("total:{}", (a.getMoney() + b.getMoney()));
    }

    /**
     * Random线程安全
     */
    static Random random = new Random();

    /**
     * 随机1~100
     * @return  1~100随机整数
     */
    public static int randomAmount() {
        return random.nextInt(100) + 1;
    }
}
